/*
Plugin Carrusel
@autor : Javier Camelis
*/
(function(jQuery){
  jQuery.widget("ui.carrusel", {
//default options
    options: {
      thumb : '',
      handlers : '',
      preview : '.preview',
      trigger_next : false,
      trigger_prev : false,
      trigger_play : false,
      trigger_stop : false,
      auto_play : false,
      timer : 5000,
      effect_time : 500,
      _nextcallback : '',
      debug : false
    },
    _init: function(o) {
        //creation code for mywidget
      var self = this, e = this.element ,o = this.options ;
      //guardar una copia para guardar usar en destroy ;
      this._old_copy = e.clone();
      
      e.css('position','relative');
      o.thumb = o.thumb.split(',');
      o.handlers = o.handlers.split(',');

      this.options.timer = this.options.timer + (this.options.effect_time * 2);
      this._clear();
      this._preview = jQuery(e.find(o.preview));
      this._preview.css('position','absolute');
      this._total = this._preview.length - 1;
//encuentro los botones
      if(!o.trigger_next) o.trigger_next = $('.next',e);
      if(!o.trigger_prev) o.trigger_prev = $('.prev',e);
      if(!o.trigger_play) o.trigger_play = e.find('.play');
      if(!o.trigger_stop) o.trigger_stop = e.find('.stop');
//defino las funciones      
      if(o.trigger_next) o.trigger_next.click(function(){self._next()});
      if(o.trigger_prev) o.trigger_prev.click(function(){self._prev()});
      if(o.trigger_play) o.trigger_play.click(function(){self.play()});
      if(o.trigger_stop) o.trigger_stop.click(function(){self.stop()});

      jQuery.each(this._preview,function(x){
          e.find(o.handlers[x]).each(function(y){
            jQuery(this).hover(
            function(){
                self.pause();
                self._lock();
                self._last = self._current;
                self._current = y;
                self._show(y);
            },function(){
              self._Timeout = setTimeout(function(){self.resume() ;},3000);
            })
          })
      })
      if(o.auto_play){
        self.play();
      }else{
        self._show(0);
      }
    },
    _clear: function(){
      var self = this;
      for(x in this.options.thumb){
        if(parseInt(x) >= 0) this.element.find(this.options.thumb[x]).removeClass('active');
      }
      if(this._last >= 0){
        this._preview.filter(':eq('+self._last+')').fadeOut(self.options.effect_time + 500)
      }else{
        jQuery(this._preview).hide();
        self._lock_out = false;
      }
    },
    _next: function(){
      this._last = this._current;
      this._current = this._current < this._total ? this._current + 1 : 0;
      this._show(this._current);
      if(this.options._nextcallback) this.options._nextcallback(this.options);
    },
    _prev: function(){
      this._last = this._current;
      this._current = this._current == 0 ? this._total : this._current - 1 ;
      this._show(this._current);
    },
    autoplay : function(){
      var self = this;
      if(this.options.auto_play){
        this._next();
        this._clear_int();
        this._interval = setTimeout(function(){self.autoplay()},this.options.timer);
      }else{
        this._clear_int();
      }
    },
    _clear_int : function(){
      if(this._interval) clearInterval(this._interval);
    },
    _clear_Tout : function(){
      if(this._Timeout) clearTimeout(this._Timeout);
    },
    play : function(){
      var self = this;
      this.options.auto_play = true ;
      this.autoplay();
      if(this._interval) this._clear_int();
      this._interval = setTimeout(function(){self.autoplay()},this.options.timer);
    },
    resume : function (){
      if(this.options.auto_play) this.play();
    },
    stop : function() {
      this._clear_int();
      this._clear_Tout();
      this._preview.filter(':eq('+this._last+')').stop().css('opacity',0);
      this._preview.filter(':eq('+this._current+')').stop().css('opacity',1);
    },
    pause : function() {
      this._clear_int();
      this._clear_Tout();
    },
    _show : function(p){
      var self = this
        this._clear();
        for(x in this.options.thumb){
          if(parseInt(x) >= 0) this.element.find(this.options.thumb[x]+':eq('+p+')').addClass('active');
        }
        this._preview.filter(':eq('+p+')').fadeIn(this.options.effect_time)
    },
    _locked : function (){
      return this._lock_in || this._lock_out ;
    },
    _lock : function(){
      this._lock_in = this._lock_out = true ;
    },
    _ready : function(){
      return !this._locked();
    },
    destroy: function() {
      jQuery.Widget.prototype.destroy.apply(this, arguments); // default destroy
      // now do other stuff particular to this widget
      this.element.replaceWith(this._old_copy)
    },
    _debug : function () {
      if(this.options.debug && 'console' in window){
        console.log(new Date().getTime());
        console.log(arguments);
      }
    },
    _current  : -1 ,
    _last : -1,
    _interval : false,
    _Timeout : false,
    _lock_in : false,
    _lock_out : false
  });
})(jQuery);